package com.lixinxin.commoncomposeproject.screen

import android.util.Log
import android.widget.Toast
import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.animateIntAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import coil.annotation.ExperimentalCoilApi
import coil.compose.rememberImagePainter
import com.lixinxin.commoncomposeproject.screen.util.Screen
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow

/**
 *   author ： lixinxin
 *   time    ： 2021-11-22
 *   email：895330766@qq.com
 */

@OptIn(ExperimentalCoilApi::class)
@Composable
fun WelcomeScreen(navController: NavController) {


    var trigger by remember { mutableStateOf(5) }

    val elapsed by animateIntAsState(
        targetValue = trigger * 1000,
        animationSpec = tween(5 * 1000, easing = LinearEasing)
    )

    DisposableEffect(Unit) {
        trigger = -1
        onDispose {
            Log.e("WelcomeScreen","${elapsed}")
        }
    }


    Surface(modifier = Modifier.fillMaxSize()) {

        Box(modifier = Modifier.fillMaxSize()) {

            Image(
                painter = rememberImagePainter(
                    data = "http://pics7.baidu.com/feed/6a63f6246b600c33175b8117b89afc09d8f9a128.jpeg?token=f1fcfda1ca3f7ba52b1bcaee2eda7835"
                ),
                contentDescription = "welcome",
                contentScale= ContentScale.FillBounds,
                modifier = Modifier
                    .fillMaxSize()
                    .clickable {
                        navController.navigate(Screen.GuideScreen.route)
                    }
            )
            Text(
                text = "${elapsed / 1000}s" ,
                Modifier
                    .clickable {
                        navController.navigate(Screen.GuideScreen.route)
                    }
                    .align(Alignment.BottomEnd)
                    .padding(bottom = 20.dp, end = 20.dp),
                textAlign = TextAlign.Center,
                fontSize = 14.sp,
                fontStyle = FontStyle.Italic
            )

            if (elapsed==0){
               // navController.navigate(Screen.GuideScreen.route)
                navController.navigate(Screen.NetworkScreen.route)
            }
        }
    }
}


